# shellcheck shell=sh
# shellcheck disable=SC2034

# ferreus_orbit Plugins for HPE "DL365 Gen11"

fo_get_sorted_name(){
  if [ "$USE_FO_NAME" -eq 1 ] || [ "$1" = "-f" ]; then
    id=$(fo_get_id|head -1)
    [ "$id" -lt 2 ] && id=1
    echo "Disk$((id - 1))"
  else
    id=$(fo_get_id)
    echo "sd$(fo_get_dev_letter "$id")"
  fi
}

fo_get_did(){
  fo_get_id
}

fo_get_id(){
  scsi=$(echo "$DEVPATH" | cut -d/ -f8| cut -d: -f3)
  if [ "$scsi" -eq "111" ]; then
    id=0
  else
    fo_raid_path=$(jq -r --arg sn "${ID_SCSI_SERIAL}" '.[]
                    |to_entries[]
                      |select(.key|tostring|endswith("attributes"))
                        |select(.value.SN== $sn ).key' \
                  "${FO_CACHE_FILE_STORCLI}" 2>/dev/null | cut -d' ' -f2)
    id=$(fo_get_slot "${fo_raid_path}")
  fi
  echo "$id"
}

fo_get_kname(){
  id=$(echo "$2" |cut -d/ -f4| awk -F 's' '{print $2}')
  [ "$id" -lt 3 ] && id=7
  echo "sd$(fo_get_dev_letter "$((id - 3))")" 
}

fo_get_foname(){
  slt=$(fo_get_slot "${1}")
  echo "Disk$((slt - 3))"
}

fo_get_slot(){
  jq -r  ".[\"Drive ${1} - Detailed Information\"]
                .\"Drive ${1} Policies/Settings\"
                  |select(.!=null)
                  .\"Connected Port Number\"" \
       "${FO_CACHE_FILE_STORCLI}" 2>/dev/null | cut -d'(' -f1 | cut -d' ' -f1
}

FO_DISPOSITION="""
HDD disposition
__{UNDERSCORE}___{UNDERSCORE}___{UNDERSCORE}__
|{Disk0}|{Disk1}|{Disk3}|
|{SN_Disk0}|{SN_Disk1}|{SN_Disk3}|
|-{MINUS}-|-{MINUS}-|-{MINUS}-|
|{Disk0}|{Disk2}|{Disk4}|
|{SN2_Disk0}|{SN_Disk2}|{SN_Disk4}|
¯¯{HIGHER}¯¯¯{HIGHER}¯¯¯{HIGHER}¯¯
"""

FO_TEXTILE="""


h3. HDD disposition


|{Disk0}|{Disk1}|{Disk3}|
|{Disk0_2}|{Disk2}|{Disk4}|
"""

fo_load_storcli
